package com.yanqu.road.server.manger.activity.qixi.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.dao.impl.player.UserQixiRelationDaoImpl;
import com.yanqu.road.entity.qixi.UserQixiRelationData;
import com.yanqu.road.pb.activity.QixiProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.qixi.QixiModule;
import com.yanqu.road.server.manger.activity.qixi.QixiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.Map;

public class QixiGetMostGiftTargetUserServiceCmd implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        QixiProto.QixiGetMostGiftTargetUserReqMsg reqMsg = QixiProto.QixiGetMostGiftTargetUserReqMsg.parseFrom(request.getDataBytes());

        long targetUserId = reqMsg.getTargetUserId();

        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(targetUserId);
        long giftMostUserId = 0;
        if (onlinePlayer != null) {
            giftMostUserId = onlinePlayer.getModule(QixiModule.class).getGiftMostUserId();
        }else{
            Map<Long, UserQixiRelationData> userQixiRelationDataMap = new UserQixiRelationDaoImpl().getUserQixiRelationDataMap(QixiMgr.getConfig().getActivityId(), targetUserId);
            giftMostUserId = QixiMgr.getGiftMostUserIdByRelationMap(userQixiRelationDataMap);
        }

        QixiProto.CrossQixiGetMostGiftTargetUserRespMsg.Builder resp = QixiProto.CrossQixiGetMostGiftTargetUserRespMsg.newBuilder();
        resp.setUserId(giftMostUserId);

        syncResponse.setResponseBuilder(resp);

        return syncResponse;
    }
}
